﻿/*
Customizer by Hoai Thanh Trinh
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using FShop.BAL;
using FShop.DAL;
public partial class EditAdvertise : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		if ( !IsPostBack )
        {
            ddlCategory.DataSource = Category_BAL.GetList_Category();
            ddlCategory.DataTextField = "Cate_Name";
            ddlCategory.DataValueField = "Cate_ID";
            ddlCategory.DataBind();
            string advertise_ID = Request.QueryString["Advertise_ID"];
            if (ExtendFunctional.IsNumberic(advertise_ID) == false)
            {
                SetControlForInsert();
            }
            else
                LoadControlForEdit(advertise_ID);
        }
        else
        {
        
        }
    }
	#region For Set control when edit/insert
    private void SetControlForInsert()
    {
        btnAdd.Visible = true;
        btnEdit.Visible = false;
        //btnBack.Attributes.Add("onclick", "javascript :history.back(1)");
    }

    private void LoadControlForEdit(string advertise_ID)
    {
        Advertise adv = Advertise_BAL.Get_Advertise_ByID(int.Parse(advertise_ID));
        if (adv != null)
        {
            if (adv.Cate_ID != int.MinValue)
            {
                ddlCategory.SelectedIndex = adv.Cate_ID;
                int index = 0;
                for (int i = 0; i < ddlPosition.Items.Count; i++)
                {
                    if (ddlCategory.Items[i].Value == adv.Adv_Position.ToString())
                    {
                        index = i;
                        break;
                    }
                }
                ddlPosition.SelectedIndex = index;
            }
            txtAdvDesc.Text = adv.Adv_Desc;
            txtAdvName.Text = adv.Adv_Title;
            txtAdvStartDate.Text = adv.Adv_CreateDate.ToString();
            txtEndDate.Text = adv.Adv_EndDate.ToString();
            txtHeight.Text = adv.Adv_Height.ToString();
            txtWidth.Text = adv.Adv_Width.ToString();
            txtAdvWebsite.Text = adv.Adv_Link;
            imgAdv.ImageUrl = "../Images/Adv/" + adv.Adv_Image.Trim();

        }
        btnAdd.Visible = false;
        btnEdit.Visible = true;
    }
    #endregion
	#region Insert/Update and upload image
    protected void Update()
    {
        string advertise_ID = Request.QueryString["Advertise_ID"];
        Advertise adv = Advertise_BAL.Get_Advertise_ByID(int.Parse(advertise_ID));
        if (adv != null)
        {
            adv.Cate_ID = int.Parse(ddlCategory.SelectedValue);
            adv.Adv_Position = int.Parse(ddlPosition.SelectedValue);
            adv.Adv_Desc = txtAdvDesc.Text;
            adv.Adv_Title = txtAdvName.Text;
            adv.Adv_CreateDate = Convert.ToDateTime(txtAdvStartDate.Text);
            adv.Adv_EndDate = Convert.ToDateTime(txtEndDate.Text);
            adv.Adv_Height = int.Parse(txtHeight.Text);
            adv.Adv_Width = int.Parse(txtWidth.Text);
            adv.Adv_Link = txtAdvWebsite.Text;
            adv.Adv_Status = true;
            if (string.IsNullOrEmpty(FileUpload1.FileName) == false)
            {                 
                string _sFolder = Request.PhysicalApplicationPath.Replace(@"\", "/") + "/Images/Adv/";
                adv.Adv_Image=ExtendFunctional.UploadPicture(_sFolder, FileUpload1.PostedFile);
            }
            Advertise_BAL.Update_Advertise(adv);
           
        }
        
    }
    protected void Insert()
    {
        Advertise adv = new Advertise();
        adv.Cate_ID = int.Parse(ddlCategory.SelectedValue);
        adv.Adv_Position = int.Parse(ddlPosition.SelectedValue);
        adv.Adv_Desc = txtAdvDesc.Text;
        adv.Adv_Title = txtAdvName.Text;
        adv.Adv_CreateDate = Convert.ToDateTime(txtAdvStartDate.Text);
        adv.Adv_EndDate = Convert.ToDateTime(txtEndDate.Text);
        adv.Adv_Height = int.Parse(txtHeight.Text);
        adv.Adv_Width = int.Parse(txtWidth.Text);
        adv.Adv_Link = txtAdvWebsite.Text;
        adv.Adv_Status = true;
        if (string.IsNullOrEmpty(FileUpload1.FileName) == false)
        {
            
            string _sFolder = Request.PhysicalApplicationPath.Replace(@"\", "/") + "/Images/Adv/";
            adv.Adv_Image = ExtendFunctional.UploadPicture(_sFolder, FileUpload1.PostedFile);
        }
        Advertise_BAL.Insert_Advertise(adv);		             

    }

    
    #endregion
	#region Button event
     protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(FileUpload1.FileName))
            {
                if (!ExtendFunctional.IsImageFile(FileUpload1.FileName))
                {
                    MessageBox.Show("Bạn chỉ được phép nhập các file ảnh.");

                    return;
                }
            }
            Insert();
			MessageBox.Show("Đã thêm mới thành công.");
        }
        catch(Exception ex)
        {
			MessageBox.Show("Thêm mới không thành công do có lỗi xảy ra.");
            throw ex;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(FileUpload1.FileName))
            {
                if (!ExtendFunctional.IsImageFile(FileUpload1.FileName))
                {
                    MessageBox.Show("Bạn chỉ được phép nhập các file ảnh.");
                    return;
                }
            }
            Update();
			MessageBox.Show("Đã cập nhật thành công.");
        }
        catch (Exception ex)
        {
			MessageBox.Show("Cập nhật không thành công do có lỗi xảy ra.");
            throw ex;
        }
    } 
	#endregion
    protected void btnBack_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect("ManageAdvertise.aspx");
    }
}

